<?php
namespace app\admin\controller;
class User extends BaseAdmin{
    public function lists(){
        if(input("get.msg")){
            $limit=input("get.limit")?input("get.limit"):10;
            $page=input("get.page")?input("get.page"):1;
            $limit=intval($limit);
            $page=intval($page);
            $start=$limit*($page-1);
            $msg = $_GET['msg'];
            if($msg==-1){
                $user = $this->db->table('admin_user')->limit($start)->order('use_id', 'desc')->pages($limit);
            }else{
                $user = $this->db->table('admin_user')->where(array('name'=>$msg))->limit($start)->order('use_id', 'desc')->pages($limit);
                if(!$user['total']){
                    $user = $this->db->table('admin_user')->where(array('phone'=>$msg))->limit($start)->order('use_id', 'desc')->pages($limit);
                    if(!$user['total']){
                        $user = $this->db->table('admin_user')->where(array('id'=>$msg))->limit($start)->order('use_id', 'desc')->pages($limit);
                    }
                }
            }
            //            dump($agent);die;
            $count = $user["total"];
            $data =array(
                'code'=>'0',
                'msg'=>'0',
                'count'=>$count,
                'data'=>array(),
            );
            foreach ($user['lists'] as $k => $v){
                $v['operation'] = '
                    <button class="layui-btn layui-btn-xs" onclick="editor('.$v['use_id'].')">编辑</button>
					<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del('.$v['use_id'].')">删除</button>
';
                if($v['use_type']==0){
                    $v['status'] = '
                        <input type="checkbox" id="'.$v['use_id'].'" name="status" lay-filter="switch" checked lay-skin="switch" lay-text="启用|禁用">
';
                }else{
                    $v['status'] = '
                        <input type="checkbox" id="'.$v['use_id'].'" name="status" lay-filter="switch" lay-skin="switch" lay-text="启用|禁用">
';
                }
                if(!empty($v['create_time'])){
                    $v['create_time'] = date("Y-m-d H:i:s",(int)$v['create_time']);
                }
                if(!empty($v['update_time'])){
                    $v['update_time'] = date("Y-m-d H:i:s",(int)$v['update_time']);
                }
                if($v['use_type']==1){
                    $v['use_type'] = '超级管理员';
                }elseif($v['use_type']==4){
                    $v['use_type'] = '业务员';
                }elseif($v['use_type']==5){
                    $v['use_type'] = '渠道';
                }
                if($v['use_sex']==0){
                    $v['use_sex'] = '男';
                }elseif($v['use_sex']==1){
                    $v['use_sex'] = '女';
                }else{
                    $v['use_sex'] = '保密';
                }
                $data['data'][] =$v;
            }
            exit(json_encode($data));die;
        }
        return $this->fetch();
    }

    //删除业务员
    public function del(){
//        exit(json_encode(input()));
        $del = $this->db->table('admin_sales')->where(array('id'=>input('post.id')))->delete();
        if($del){
            exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
        }else{
            exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
        }
    }

    //修改业务员
    public function add(){
//        exit(json_encode(input('get.id')));
        if(input('get.id')){
            $user = $this->db->table('admin_user')->where(array('use_id'=>input('get.id')))->item();
//                    exit(json_encode($sales));
            $this->assign('user',$user);
            return $this->fetch();
        }else{
            $sales = [
                'id' => '',
                'name' => '',
                'phone' => '',
                'id_card' => '',
            ];
            $this->assign('sales',$sales);
            return $this->fetch();
        }
    }

    //保存
    public function save(){
        $id = input('post.id');
        $data = input();
        $data['addtime'] = time();
        if($id){
            $save = $this->db->table('admin_sales')->where(array('id'=>$id))->update($data);
            exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
        }else{
            $save = $this->db->table('admin_sales')->insert($data);
            exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
        }
        exit(json_encode(input()));
    }

    public function status(){
        exit(json_encode(array('code'=>1,'msg'=>'修改失败')));
        //exit(json_encode($_POST));
        $_POST['status'] = $_POST['status']=='true'?0:1;
        $save = $this->db->table('sales')->where(array('id'=>$_POST['id']))->update($_POST);
        if($save){
            exit(json_encode(array('code'=>0,'msg'=>'修改成功')));
        }else{
            exit(json_encode(array('code'=>1,'msg'=>'修改失败')));
        }
    }

    public function upload(){
//        if ($_FILES["file"]["error"] > 0)
//        {
//            echo "错误：" . $_FILES["file"]["error"] . "<br>";
//        }
//        else
//        {
//            echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
//            echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
//            echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
//            echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];
//        }die;
        //dump(DOCUMENT_ROOT.'/public/upload');die;
        //判断id是否存在，不存在获取用户id找到最后房源id，添加url
        $file = request()->file('file');
        if($file==null){
            exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
        }
        $info = $file->move(DOCUMENT_ROOT.'\public\upload');
        $ext = ($info->getExtension());
        if(!in_array($ext,array('jpg','jpeg','gif','png'))){
            exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
        }
        $img = '/upload/'.$info->getSaveName();
        exit(json_encode(array('code'=>0,'msg'=>$img)));
    }
}